<template>
	<view class="content">
		<view class="card">
			<card :name="notice.admin.name" :data="notice.notice.createTime" :avatar="'http://localhost:4567/messy_json/img?path='+notice.admin.avatar"
			 :content="notice.notice.content" :level="'管理员'">
			</card>
		</view>
		<view class="funcs">
			<funcs></funcs>
		</view>
		<navigator url="/pages/login/login" open-type="navigate" v-if="librarys == ''">
			<button type="default">请先登录</button>
		</navigator>
		<view class="librarys">
			<view class="libray-item" v-for="(item,i) in librarys" :key="item.id" @click="libraryInfo(item.id)">
				<card :name="item.libraryName" :data="item.createTime" :avatar="'http://localhost:4567/messy_json/img?path='+item.img"
				 :content="item.message" :contentImg="'http://localhost:4567/messy_json/img?path='+item.img">
				</card>
			</view>
		</view>
		<view style="width:100%;">
		    <official-account></official-account>
		</view>
	</view>
</template>

<script>
	import card from "@/components/card/card.vue"
	import funcs from "@/components/home-control/home-funcs.vue"
	export default {
		components: {
			card,
			funcs,
		},
		data() {
			return {
				notice: "",
				librarys: ""
			};
		},
		onLoad() {
			var _self = this;
			this.notice = "";
			//初始化公告
			this.getNotice();
			this.getLibraryList();
		},
		onPullDownRefresh() {
			this.getNotice();
			this.getLibraryList();
		},
		methods: {
			libraryInfo(id) {
				var link = "/pages/libraryInfo/libraryInfo?id=" + id;
				uni.navigateTo({
					url: link
				});
			},
			getNotice() {
				var _self = this;
				_self.notice = "";
				uni.request({
					url: "http://localhost:4567/messy_json/notice.json",
					method: "POST",
					success: (res) => {
						_self.notice = res.data;
					}
				});
			},
			getLibraryList() {
				var _self = this;
				uni.getStorage({
					key: 'token',
					success: function(storage) {
						uni.request({
							url: "http://localhost:4567/library_json/list.json",
							header: {
								"token": storage.data.tokenName,
							},
							method: "POST",
							success: (res) => {
								if (res.statusCode != 200) {
									uni.navigateTo({
										url: '/pages/login/login'
									});
								} else {
									_self.librarys = res.data
								}
							},
							fail: (res) => {
								uni.navigateTo({
									url: '/pages/login/login'
								});
							}

						});
					},
					fail: function(res) {
						uni.navigateTo({
							url: '/pages/login/login'
						});
					}
				});
			},
		}
	};
</script>

<style lang="less" scoped>
	.content {
		padding-top: 30rpx;
		padding-bottom: 30rpx;
		background-color: #FCFAFA;

		.card {
			width: 90%;
			margin: 0 auto;
		}

		.funcs {
			background-color: #FFFFFF;
			margin-top: 30rpx;
		}
	}

	.librarys {
		width: 98%;
		margin: 0 auto;

		.libray-item {
			width: 46%;
			margin: 2%;
			display: inline-block;
		}
	}
</style>
